package com.csx.base.design.behavior.strategy.example.exampleB;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * 某业务服务类
 * @author cuisongxu
 * @date 2022/9/30 11:44
 */
@Service
public class BizService {

    @Autowired
    private BizUnitService bizUnitService;

    private Map<String, Function<String, String>> checkResultDispatcherComX = new HashMap<>();

    @PostConstruct
    public void checkResultDispatcherComXInit() {
        checkResultDispatcherComX.put("key_订单1", order -> bizUnitService.bizOne(order));
        checkResultDispatcherComX.put("key_订单1_订单2", order -> bizUnitService.bizTwo(order));
        checkResultDispatcherComX.put("key_订单1_订单2_订单3", order -> bizUnitService.bizThree(order));
    }

    public String getCheckResultComX(String order, int level) {
        String key = getDispatcherComXKey(order, level);
        Function<String, String> result = checkResultDispatcherComX.get(key);
        if(result != null) {
            // 执行表达式获取结果
            return result.apply(order);
        }
        return "不在处理的逻辑中，返回业务错误";
    }

    private String getDispatcherComXKey(String oder, int level) {
        StringBuilder key = new StringBuilder("key");
        for (int i = 1; i <= level; i++) {
            key.append("_").append(oder).append(i);
        }
        return key.toString();
    }
}
